package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.BannerNews;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.request.banner.OnShelfRequest;
import com.dianxin.farm.request.banner.QueryBannerRequest;
import com.dianxin.farm.request.common.IdRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.service.IBannerNewsService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 橱窗信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/bannerNews", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@AuthorCheck
@Api(description = "公告控制器")
public class BannerNewsController {
    @Resource
    private IBannerNewsService bannerNewsService;
    @Resource
    private CurrentLoginService currentLoginService;


    @ApiOperation("发布或者修改公告，发布时候id请传0或者不传。默认为草稿形态")
    @RequestMapping(value = "/publishBanner")
    @ResponseBody
    @AuthorCheck
    public ApiResponse publishBanner(@RequestBody BannerNews request) {
        Integer creatorId = currentLoginService.getId();
        request.setCreatorId(creatorId);
        request.setCreateTime(LocalDateTime.now());
        bannerNewsService.saveOrUpdate(request);
        return ApiResponseUtils.success();
    }


    @ApiOperation("上下架公告=======")
    @RequestMapping(value = "/shelf")
    @AuthorCheck
    public ApiResponse shelf(@RequestBody OnShelfRequest request) {

        Integer bannerId=request.getBannerId();
        if (bannerId==null){
            throw new UserOperationException("请选择要修改的公告");
        }
        Integer shelf = request.getShelf();
        if (shelf!=0 && shelf!=1){
            throw new UserOperationException("参数异常");
        }
        if (shelf != null) {
            BannerNews bannerNews = bannerNewsService.getById(bannerId);
            if (bannerNews != null) {
                bannerNews.setShelf(shelf);
                bannerNewsService.updateById(bannerNews);
            }
        }
        return ApiResponseUtils.success();
    }


    @ApiOperation("公告分页列表")
    @RequestMapping(value = "/bannerPages")
    @ResponseBody
    @AuthorCheck
    public ApiResponse<PageInfo<BannerNews>> bannerPages(@Valid @RequestBody QueryBannerRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        Integer shelf = request.getShelf();
        String heading = request.getHeading();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(shelf != null, "shelf", shelf);
        if (heading != null && !heading.equals("")) {
            wrapper.like("heading", heading);
        }

        wrapper.orderByDesc("id");

        List<BannerNews> newsList = bannerNewsService.list(wrapper);
        return ApiResponseUtils.success(new PageInfo<>(newsList));
    }

    @ApiOperation("删除公告")
@RequestMapping(value = "/remove")
@ResponseBody
@AuthorCheck
public ApiResponse remove(@Valid @RequestBody IdRequest request) {
        Integer id=request.getId();
        bannerNewsService.removeById(id);
        return ApiResponseUtils.success();
        }


        }
